package bb.lanxing.lib.devices.utils;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.ParcelUuid;
import androidx.core.content.ContextCompat;
//import gov.nist.core.Separators;
import bb.lanxing.activity.deeplink.DeepLinkPathConstants;
import bb.lanxing.activity.interfaces.Separators;
import bb.lanxing.lib.devices.api.SmartDevice;
import java.util.UUID;
import android.util.Log;


public class BleUtils {
    public static final int DATA_TYPE_MANUFACTURER_SPECIFIC_DATA = 255;

    public static void requestBattery(Context context, int i, String str) {
        Intent intent = new Intent("ACTION_REQUEST_BATTERY");
        intent.putExtra("EXTRA_DEVICE_TYPE", i);
        intent.putExtra("EXTRA_DEVICE_ADDRESS", str);
        context.sendOrderedBroadcast(intent, null);
    }

    public static void requestFirmwareVersion(Context context, int i, String str) {
        Intent intent = new Intent("ACTION_REQUEST_DEVICE_INFORMATION");
        intent.putExtra("EXTRA_DEVICE_TYPE", i);
        intent.putExtra("EXTRA_DEVICE_ADDRESS", str);
        context.sendOrderedBroadcast(intent, null);
    }

    public static int getDeviceType(SmartDevice smartDevice, byte[] bArr) {
        handelNameForCBL(smartDevice);
        return getDeviceType(smartDevice.getName().toLowerCase().trim(), bArr);
    }
    public static int getDeviceType(String str, byte[] bArr) {
        //FTMS Indoor Bike
        if (DeviceUtils.isIndoorBikeDevice(str, bArr)) {
            return SmartDevice.TYPE_INDOOR_BIKE;
        }
        if (DeviceUtils.isIndoorBikeAdvertising(str, bArr)) {
            return SmartDevice.TYPE_INDOOR_BIKE;
        }
        //FTMS Cross Trainer
        if (DeviceUtils.isCrossTrainerAdvertising(str, bArr)) {
            return SmartDevice.TYPE_CROSS_TRAINER;
        }
        if (DeviceUtils.isCrossTrainerDevice(str, bArr)) {
            return SmartDevice.TYPE_CROSS_TRAINER;
        }
        //FTMS Rower
        if (DeviceUtils.isRowerDevice(str, bArr)) {
            return SmartDevice.TYPE_ROWER;
        }

        if (DeviceUtils.isRowerAdvertising(str, bArr)) {
            return SmartDevice.TYPE_ROWER;
        }

        //Bike Trainer
        if (DeviceUtils.isBikeTrainerDevice(str, bArr)) {
            return SmartDevice.TYPE_BIKE_TRAINER;
        }
        //Power device
        if (DeviceUtils.isPowerRateDevice(str, bArr)) {
            return SmartDevice.TYPE_POWERRATE;
        }

        if (DeviceUtils.isQi(str)) {
            return SmartDevice.TYPE_Q1;
        }
        if (str.startsWith(DeepLinkPathConstants.SCHEME_APP_LANXING) && str.contains("x1")) {
            return  SmartDevice.TYPE_X1;
        }
        if (str.contains("igs")) {
            return SmartDevice.TYPE_IGPS;
        }
        if (str.contains("rider")) {
            return SmartDevice.TYPE_BRYTEN;
        }
        if (str.contains("xoss_v") || str.contains("dfu_v")) {
            return SmartDevice.TYPE_VORTEX_CADENCE;//TYPE_SENSOR_HEART_RATE
        }
        if (DeviceUtils.isXossGDevice(str)) {
            return SmartDevice.TYPE_XOSS_G;
        }
        if (DeviceUtils.isXossNav(str)) {
            return SmartDevice.TYPE_XOSS_NAV;
        }
        if (str.contains("m1") || str.contains("dfu_m1") || str.contains("m1b") || str.contains("dfu_m1b")) {
            return SmartDevice.TYPE_CYCLE_PLUS_M1;
        }
        if (str.contains("m2") || str.contains("dfu_m2")) {
            return SmartDevice.TYPE_CYCLE_PLUS_M2;
        }
        if (str.contains("cl_600") || str.contains("dfu_600")) {
            return SmartDevice.TYPE_CHILEAF_600;
        }
        if (str.contains("xoss_x2p") || str.contains("dfu_x2p")) {
            return SmartDevice.TYPE_HR_X2_PRO;
        }
        if (DeviceUtils.isDiscoveryDevice(bArr)) {
            return SmartDevice.TYPE_DISCOVERY;
        }
//        if (DeviceUtils.isSprintDevice(str, bArr)) {//TODO: remove not used, heart belt sensor will be recognized as SprintDevice
//            return SmartDevice.TYPE_WINGS;
//        }
        if (DeviceUtils.isBici(bArr)) {
            return SmartDevice.TYPE_BICI;
        }
        if (str.contains("sr1")) {
            return SmartDevice.TYPE_RADAR_SR;
        }
        if (DeviceUtils.isHeartRate01Device(str, bArr)) {
            return SmartDevice.TYPE_HEART_RATE01;
        }
        if (DeviceUtils.isHeartRateDevice(str, bArr)) {
            return SmartDevice.TYPE_HEARTRATE;
        }
        if (DeviceUtils.isCadence01Device(str, bArr)) {
            return SmartDevice.TYPE_CADENCE01;
        }
        if (DeviceUtils.isCadenceDevice(bArr)) {
            return SmartDevice.TYPE_CADENCE;
        }
        if (DeviceUtils.isHeartRateBeltDevice(str, bArr)) {
            return SmartDevice.TYPE_HEARTRATE_BELT;
        }
        return DeviceUtils.isLightDevice(str, bArr) ? SmartDevice.TYPE_LIGHT : SmartDevice.TYPE_UNKNOW;
    }

    public static void handelNameForCBL(SmartDevice smartDevice) {
        if (smartDevice.getName().toLowerCase().contains("cbl")) {
            smartDevice.setName(smartDevice.getName().replaceFirst("M[0-9a-zA-z]\\s", ""));
            smartDevice.setName(smartDevice.getName().replaceFirst("M[0-9a-zA-z]-[0-9a-zA-z]{2}\\s", ""));
        }
    }

    public static boolean containsUUID(ParcelUuid[] parcelUuidArr, UUID uuid) {
        boolean z = false;
        for (ParcelUuid parcelUuid : parcelUuidArr) {
            if (parcelUuid.getUuid().compareTo(uuid) == 0) {
                z = true;
            }
        }
        return z;
    }

    public static long macAddrToLong(String str) {
        if (str == null) {
            return -1L;
        }
        return Long.parseLong(str.replaceAll(Separators.COLON, ""), 16);
    }

    public static String longToMacAddr(long j) {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 5; i >= 0; i--) {
            String hexString = Long.toHexString((j >> (i << 3)) & 255);
            if (hexString.length() == 1) {
                stringBuffer.append("0");
            }
            stringBuffer.append(hexString);
            if (i > 0) {
                stringBuffer.append(Separators.COLON);
            }
        }
        return stringBuffer.toString().toUpperCase();
    }

    public static String incrementAddress(String str) {
        return longToMacAddr(macAddrToLong(str) + 1);
    }

    public static String decrementAddress(String str) {
        return longToMacAddr(macAddrToLong(str) - 1);
    }

    public static byte[] extractScanRecord(byte[] bArr, int i) {
        byte[] bArr2 = null;
        if (bArr != null && bArr.length != 0) {
            int i2 = 0;
            while (i2 < bArr.length) {
                int i3 = i2 + 1;
                int i4 = bArr[i2] & 255;
                if (i4 == 0) {
                    break;
                }
                int i5 = i4 - 1;
                int i6 = i3 + 1;
                int i7 = bArr[i3] & 255;
                if (i7 == i && i7 == 255) {
                    bArr2 = extractBytes(bArr, i6, i5);
                }
                i2 = i5 + i6;
            }
        }
        return bArr2;
    }

    private static byte[] extractBytes(byte[] bArr, int i, int i2) {
        byte[] bArr2 = new byte[i2];
        System.arraycopy(bArr, i, bArr2, 0, i2);
        return bArr2;
    }

    public static boolean checkBluetooth(Context context) {
        int checkSelfPermission;
        int checkSelfPermission2;
        BluetoothManager bluetoothManager;
        boolean z = true;
        boolean z2 = Build.VERSION.SDK_INT >= 18;
        if (z2) {
            if (Build.VERSION.SDK_INT >= 31) {
                checkSelfPermission = ContextCompat.checkSelfPermission(context, "android.permission.BLUETOOTH_SCAN");
                checkSelfPermission2 = ContextCompat.checkSelfPermission(context, "android.permission.BLUETOOTH_CONNECT");
            } else {
                checkSelfPermission = ContextCompat.checkSelfPermission(context, "android.permission.BLUETOOTH");
                checkSelfPermission2 = ContextCompat.checkSelfPermission(context, "android.permission.BLUETOOTH_ADMIN");
            }
            if ((checkSelfPermission | checkSelfPermission2) != 0 || (bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE)) == null) {
                return z2;
            }
            BluetoothAdapter adapter = bluetoothManager.getAdapter();
            if (adapter == null || !adapter.isEnabled()) {
                z = false;
            }
            return z;
        }
        return z2;
    }

    public static boolean isBluetoothEnable(Context context) {
        BluetoothAdapter adapter = ((BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter();
        return adapter != null && adapter.isEnabled();
    }
}
